import com.sc.server.member.config.LoginContext;
import com.sc.server.member.controller.MemberController;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @author yuantongqin
 * desc:
 * 2020-08-11
 */
public class Ta {

    @Test
    public void aa(){
        LoginContext build = LoginContext.builder().setUserId(1L).build();
        System.out.println("哈哈哈=="+LoginContext.getUserId());
        Long userId = build.getUserId();
        System.out.println("userId=="+userId);

        new Thread(()->{
            LoginContext.builder().setUserId(2L).build();
            System.out.println("hhh=="+LoginContext.getUserId());
        }).start();


        System.out.println("结尾=="+LoginContext.getUserId());
    }

    @Autowired
    RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Test
    public void bb(){
        Class<?> beanType = null;
        Class<?> aClass = MemberController.class;
        // 判断一个类上是否有注解
        boolean b = AnnotatedElementUtils.hasAnnotation(aClass, Controller.class);
        System.out.println(b);

        // 判断一个方式上是否有注解
        Stream.of(aClass.getDeclaredMethods()).forEach(element->{
            RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
            System.out.println(requestMapping);
        });


        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        handlerMethods.forEach((key, value)->{
            Set<String> patterns = key.getPatternsCondition().getPatterns();
            patterns.forEach(info->{
                System.out.println(info);
            });
        });



    }
}
